<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class trouble_remedy_droplist extends Page
{
       public $btnDeleteRemedy = null;
       public $btnUpdateRemedy = null;
       public $btnAddRemedy = null;
       public $JSDeleteTrouble = null;
       public $hfRemedy = null;
       public $hfTrouble = null;
       public $editRemedy = null;
       public $editTrouble = null;
       public $Label2 = null;
       public $Label1 = null;
       public $btnDeleteTrouble = null;
       public $btnUpdateTrouble = null;
       public $btnAddTrouble = null;
       public $JTSiteTheme1 = null;
       public $JTRemedy = null;
       public $JTTrouble = null;
       function btnDeleteRemedyJSClick($sender, $params)
       {

       ?>
       var remedy_id = document.getElementById('hfRemedy').value;
       if(remedy_id=='' || remedy_id==null)
       {
        alert('Please select a remedy first');
       }
       if(remedy_id!='' && remedy_id!=null)
       {
        deleteRemedy();
       }
       <?php

       }

       function btnUpdateRemedyClick($sender, $params)
       {
        $remedy_id = $this->hfRemedy->getValue();
        if($remedy_id=='' || $remedy_id==null)
        {
         echo "<script type='text/javascript'>
         alert('Please select a remedy first');
         </script>";
        }
        if($remedy_id!='' && $remedy_id!=null)
        {
         $query_update_remedy = "UPDATE remedy_droplist_tbl SET value='".$this->editRemedy->Text."'
         WHERE autoid='".$remedy_id."'";
         mysql_query($query_update_remedy) or die("UPDATE REMEDY ".mysql_error());

         echo "<script type='text/javascript'>
         document.location.href = 'trouble_remedy_droplist.php';
         </script>";
        }


       }

       function btnAddRemedyClick($sender, $params)
       {
        $t_id = $this->hfTrouble->getValue();
        if($t_id=='' || $t_id==null)
        {
         echo "<script type='text/javascript'>
         alert('Please select a trouble first');
         </script>
         ";
        }
        if($t_id!='' && $t_id!=null)
        {
         $query_insert_remedy = "INSERT INTO remedy_droplist_tbl
         (company_id,trouble_droplist_id,value)
         VALUES
         ('".$_SESSION['company_id']."','".$t_id."','".$this->editRemedy->Text."')";
         mysql_query($query_insert_remedy) or die("INSERT REMEDY ".mysql_error());
         redirect("trouble_remedy_droplist.php?trouble_id=".$t_id."&remedy_id=0");
        }
       }
       function JTRemedyShow($sender, $params)
       {
        $get_remedy_id = $this->hfRemedy->getValue();
        $trouble_id = $this->hfTrouble->getValue();
        $query_remedy = "SELECT * FROM remedy_droplist_tbl
        WHERE trouble_droplist_id='".$this->hfTrouble->getValue()."'";
        //die($query_remedy);
        $result_remedy = mysql_query($query_remedy) or die("REMEDY LIST".mysql_error());
        $grid_arr = array();
        while($row_remedy = mysql_fetch_array($result_remedy))
        {
         $remedy_id = $row_remedy['autoid'];
         if($remedy_id==$get_remedy_id)
         {
          $value = "<a style='background-color:yellow' href=trouble_remedy_droplist.php?trouble_id=".$trouble_id."&remedy_id=".$remedy_id.">".$row_remedy["value"].
          "</a>";
         }
         if($remedy_id!=$get_remedy_id)
         {
          $value = "<a href=trouble_remedy_droplist.php?trouble_id=".$trouble_id."&remedy_id=".$remedy_id.">".$row_remedy["value"].
          "</a>";
        }
         $grid_arr[] = array(
         "Remedy"=>$value
         );
        }
        $this->JTRemedy->setCellData($grid_arr);
       }


       function btnDeleteTroubleJSClick($sender, $params)
       {

       ?>
       var trouble_id = document.getElementById('hfTrouble').value;
       if(trouble_id=='' || trouble_id==null)
       {
        alert('Please select a trouble first');
       }
       if(trouble_id!='' && trouble_id!=null)
       {
        deleteTrouble();
       }
       <?php

       }

       function btnUpdateTroubleClick($sender, $params)
       {
        $trouble_id = $this->hfTrouble->getValue();
        if($trouble_id=='' || $trouble_id==null)
        {
         echo "<script type='text/javascript'>
         alert('Please select a trouble first');
         </script>";
        }
        if($trouble_id!='' && $trouble_id!=null)
        {
         $query_update_trouble = "UPDATE trouble_droplist_tbl SET value='".$this->editTrouble->Text."'
         WHERE autoid='".$trouble_id."'";
         mysql_query($query_update_trouble) or die("UPDATE TROUBLE ".mysql_error());

         echo "<script type='text/javascript'>
         document.location.href = 'trouble_remedy_droplist.php';
         </script>";
        }
       }


       function btnAddTroubleClick($sender, $params)
       {
        $query_insert_trouble = "INSERT INTO trouble_droplist_tbl
        (company_id,value) VALUES
        ('".$_SESSION['company_id']."','".$this->editTrouble->Text."')";
        mysql_query($query_insert_trouble) or die("INSERT TROUBLE ".mysql_error());

        redirect("trouble_remedy_droplist.php?trouble_id=0&remedy_id=0");
        /*echo "<script type='text/javascript'>
        document.location.href = 'trouble_remedy_droplist.php';
        </script>";*/
       }


       function JTTroubleShow($sender, $params)
       {
       $company_id = $_SESSION['company_id'];
       $get_trouble_id = $this->hfTrouble->getValue();
       $query_trouble = "SELECT * FROM trouble_droplist_tbl WHERE company_id='".$company_id."'";
       $result_trouble = mysql_query($query_trouble) or die("SELECT TROUBLE DROP LIST ".mysql_error());

       $grid_arr = array();
       while($row_trouble = mysql_fetch_array($result_trouble))
       {
        $trouble_id = $row_trouble["autoid"];
        if($trouble_id==$get_trouble_id)
        {
         $value = "<a style='background-color:yellow' href=trouble_remedy_droplist.php?trouble_id=".$trouble_id.">".$row_trouble["value"].
         "</a>";
        }
        if($trouble_id!=$get_trouble_id)
        {
         $value = "<a href=trouble_remedy_droplist.php?trouble_id=".$trouble_id.">".$row_trouble["value"].
         "</a>";
        }
        $grid_arr[] = array("Trouble"=>$value
        );
       }
       $this->JTTrouble->setCellData($grid_arr);
       }
       function trouble_remedy_droplistCreate($sender, $params)
       {
        $trouble_id = $_GET['trouble_id'];
        $remedy_id = $_GET['remedy_id'];

        if($trouble_id!='' && $trouble_id!=null)
        $this->hfTrouble->setValue($trouble_id);

        if($remedy_id!='' && $remedy_id!=null)
        $this->hfRemedy->setValue($remedy_id);

        //DELETE TROUBLE(CHECK IF TROUBLE STILL EXIST FOR REMEDY)
        $hf_trouble = $this->hfTrouble->getValue();
        $query_check_trouble = "SELECT autoid FROM trouble_droplist_tbl where autoid='".$hf_trouble."'";
        $result_check_trouble = mysql_query($query_check_trouble) or die("SELECT CHECK TROUBLE VALUE ".mysql_error());
        $count_check_trouble = mysql_num_rows($result_check_trouble);
        if($count_check_trouble==0)
        {
        $this->hfTrouble->setValue('');
        }

        $query_trouble = "SELECT value FROM trouble_droplist_tbl WHERE autoid='".$trouble_id."'";
        $result_trouble = mysql_query($query_trouble) or die("SELECT TROUBLE VALUE ".mysql_error());
        $row_trouble = mysql_fetch_array($result_trouble);
        $this->editTrouble->Text = $row_trouble["value"];

        $query_remedy = "SELECT value FROM remedy_droplist_tbl WHERE autoid='".$remedy_id."'";
        $result_remedy = mysql_query($query_remedy) or die("SELECT REMEDY VALUE ".mysql_error());
        $row_remedy = mysql_fetch_array($result_remedy);
        $this->editRemedy->Text = $row_remedy["value"];
       }

}

global $application;

global $trouble_remedy_droplist;

//Creates the form
$trouble_remedy_droplist=new trouble_remedy_droplist($application);

//Read from resource file
$trouble_remedy_droplist->loadResource(__FILE__);

//Shows the form
$trouble_remedy_droplist->show();

?>